#include"taskQueue.h"
int taskQueueInit(taskQueue_t *pqueue){
    bzero(pqueue,sizeof(taskQueue_t));
    pqueue->pFront =NULL;
    pqueue->pRear = NULL;
    pqueue->queueSize = 0;
    return 0;
}
int enQueue(taskQueue_t *pqueue,const task_t task){
    //新建节点
    node_t* pNew = (node_t*)calloc(1,sizeof(node_t));
    pNew->task.netfd = task.netfd;
    memset(pNew->task.username,0,sizeof(pNew->task.username));
    memcpy(pNew->task.username,task.username,strlen(task.username)+1);
    memset(pNew->task.command,0,sizeof(pNew->task.command));
    memcpy(pNew->task.command,task.command,strlen(task.command)+1);
    //插入队列
    if(pqueue->queueSize == 0){
        pqueue->pFront = pNew;
        pqueue->pRear = pNew;
    }else{
        pqueue->pRear->pNext = pNew;
        pqueue->pRear = pNew;
    }
    ++pqueue->queueSize;
    return 0;
}

int deQueue(taskQueue_t *pqueue,task_t* ptask){
    node_t* pCur = pqueue->pFront;
    if(pCur == NULL){
        //队列为空
        ptask = NULL;
        return -1;
    }
    //取出节点
    ptask->netfd = pCur->task.netfd;
    memset(ptask->username,0,sizeof(pCur->task.username));
    memcpy(ptask->username,pCur->task.username,strlen(pCur->task.username)+1);
    memset(ptask->command,0,sizeof(pCur->task.command));
    memcpy(ptask->command,pCur->task.command,strlen(pCur->task.command)+1);
    //从队列中删除节点
    pqueue->pFront = pCur->pNext;
    if(pqueue->queueSize == 1){
        pqueue->pRear =NULL;
    }
    free(pCur);
    --pqueue->queueSize;
    return 0;
}
